﻿using System.Collections.Generic;
using System.Linq;
using System.Text;
using CrateMayhem.Common.Model;

namespace CrateMayhem.Common.Data
{
    public abstract class WorldRepositoryBase : IWorldRepository
    {
        protected static readonly List<World> Worlds = new List<World>();

        public IEnumerable<World> LoadAll()
        {
            return Worlds;
        }

        public World Find(int level)
        {
            return Worlds.FirstOrDefault(w => w.Level == level);
        }

        protected static World Create(List<string> data)
        {
            var map = new StringBuilder();
            var level = int.Parse(data.First(d => d.StartsWith("Title: Level ")).Replace("Title: Level ", ""));
            var solution = data.First(d => d.StartsWith("Solution: ")).Replace("Solution: ", "");
            
            foreach (var d in data.Where(d => d.Contains("#")).ToList())
            {
                map.AppendLine(d);
            }

            return new World(level, map.ToString(), solution);
        }
    }
}
